using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace preThreadedTCPServer
{
public class ThreadPoolTcpSrvr
{
   private TcpListener client;

   public ThreadPoolTcpSrvr(int port)
   {
      client = new TcpListener(port);
      client.Start();

      Console.WriteLine("Waiting for clients...");
      while(true)
      {
         while (!client.Pending())
         {
            Thread.Sleep(1000);
         }
         ConnectionThread newconnection = new ConnectionThread();
         newconnection.threadListener = this.client;
         ThreadPool.QueueUserWorkItem(new
                    WaitCallback(newconnection.HandleConnection));
      }
   }

   public static void Main(string[] args)
   {
		if (args.Length == 0)
			{
				Console.WriteLine("enter a port number you want the server to work on");
				Environment.Exit(1);
			}
		else{
				int portnumber = int.Parse(args[0]);
				ThreadPoolTcpSrvr tpts = new ThreadPoolTcpSrvr(portnumber);
      			
			}
   }
}

class ConnectionThread
{
   public TcpListener threadListener;
   private static int connections = 0;

   public void HandleConnection(object state)
   {
      int recv;
      byte[] data = new byte[1024];

      TcpClient client = threadListener.AcceptTcpClient();
      NetworkStream ns = client.GetStream();
      connections++;
      Console.WriteLine("New client accepted: {0} active connections",
                         connections);

      string welcome = "Welcome to the prethreaded C# server";
      data = Encoding.ASCII.GetBytes(welcome);
      ns.Write(data, 0, data.Length);

      while(true)
      {
         data = new byte[1024];
         recv = ns.Read(data, 0, data.Length);
         if (recv == 0)
            break;
      
         ns.Write(data, 0, recv);
      }
      ns.Close();
      client.Close();
      connections--;
      Console.WriteLine("Client disconnected: {0} active connections",
                         connections);
   }
}
}